package com.fc.redap.controller.api;

import com.fc.redap.dtos.ResetUserPassRequest;
import com.fc.redap.dtos.ResponseJson;
import com.fc.redap.entity.SMSModule;
import com.fc.redap.entity.SmsHistoryEntity;
import com.fc.redap.entity.UserEntity;
import com.fc.redap.helper.StringUtils;
import com.fc.redap.service.ISmsHistoryService;
import com.fc.redap.service.IUserService;
import com.fc.redap.utils.annotation.IgnoreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/api")
@Api(value="手机短信相关的API", tags={"短信录接口"})
public class SmsController {

    @Autowired
    private ISmsHistoryService iSmsHistoryService;

    @Autowired
    private IUserService iUserService;

    @IgnoreAuth
    @PostMapping("/sms")
    @ApiOperation(value = "发送短信",
            notes = "module: <ul>" +
                    "<li><i>register: 注册</i></li>" +
                    "<li><i>resetPwd: 密码重置</i></li>" +
                    "<li><i>recharge: 充值</i></li>" +
                    "<li><i>verifyCode: 验证码</i></li>" +
                    "<li><i>other: 其它</i></li>" +
                    "</ul>",
            response = ResponseJson.class,
            httpMethod = "POST",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "mobile", value = "手机号"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "module", value = "模块", required = true)
    })
    public ResponseJson sendVerifyCode(String mobile, String module){
        ResponseJson responseJson = new ResponseJson();
        SMSModule smsModule = SMSModule.fromString(module);
        if (StringUtils.isEmpty(mobile))
        {
            //用户已注册的用户则直接将短信发送到已注册的手机号
            UserEntity userEntity = iUserService.queryByMobile(mobile);
            if (null != userEntity)
            {
                mobile = userEntity.getPhone();
            }
            else
            {
                responseJson.setSuccess(false);
                responseJson.setMsg("手机号不存在");
                return responseJson;
            }
        }
        iSmsHistoryService.send(mobile, smsModule);
        responseJson.setSuccess(true);
        responseJson.setMsg("");
        return responseJson;
    }


    @PostMapping("/sms/password/reset")
    @ApiOperation(value = "重置密码",
            notes = " REST 方式登录。" +
                    "使用方式: <ul>" +
                    "<li><i>phone: 手机号</i></li>" +
                    "<li><i>newPassword: 新密码</i></li>" +
                    "<li><i>verifyCode: 短信验证码</i></li>" +
                    "</ul>",
            response = ResponseJson.class,
            httpMethod = "POST",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    public ResponseJson handlePostResetPassword(@RequestBody ResetUserPassRequest request)
    {
        ResponseJson resp = new ResponseJson();
        UserEntity user = iUserService.queryByMobile(request.getPhone());
        if (user != null)
        {
            SmsHistoryEntity smsHistory = iSmsHistoryService.queryByMobileAndCode(request.getPhone(),request.getVerifyCode());
            if (null != smsHistory)
            {
                user.setPassword(DigestUtils.sha256Hex(request.getNewPassword()));
                user.setUpdateTime(new Date());
                iUserService.update(user);
                resp.setMsg("密码重置成功");
            }
            else
            {
                resp.setSuccess(false);
                resp.setMsg("验证码不正确");
                return resp;
            }
        }

        return  resp;
    }

}
